#include <GL/glut.h>
#include <stdlib.h>
#define min(a,b) ((a) > (b) ? (b) : (a))

static GLfloat spin = 0.0;
double r = 1.0;
double l = 10.0;
double spin_inc = 0.0;

void display(void)
{
   GLUquadricObj* q = gluNewQuadric();
   

   glClear(GL_COLOR_BUFFER_BIT);
   glPushMatrix();
   glRotatef(spin, 0.0, 0.0, 1.0);
   glColor3f(1.0, 1.0, 1.0);
   glBegin(GL_LINE);
       glVertex2d(0.0, 0.0);
       glVertex2d(0.0, -1.0*l);
   glEnd();
   glTranslatef(0.0, -10.0, 0.0);
   gluDisk(q, 0.0, r, 16, 1);
   glPopMatrix();

   glutSwapBuffers();
}

void spinDisplay(void)
{
   spin = spin+spin_inc;
   if (spin > 360.0)
      spin = spin - 360.0;
   glutPostRedisplay();
}

void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
}

void reshape(int w, int h)
{
   int s = min(w, h);
   glViewport (0, 0, (GLsizei) s, (GLsizei) s);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-1.25*l, 1.25*l, -1.25*l, 1.25*l, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void menuResponse(int value)
{
    switch(value){
        case '+':
            spin_inc = 0.3;
            glutIdleFunc(spinDisplay);
            break;
        case '-':
            spin_inc = -0.3;
            glutIdleFunc(spinDisplay);
            break;
        case ' ':
            glutIdleFunc(NULL);
            break;
        case 27:
            exit(0);
            break;    
    }

}

void keypress(unsigned char key, int x, int y)
{
    menuResponse(key);
}

void mouse(int button, int state, int x, int y) 
{
   switch (button) {
      case GLUT_LEFT_BUTTON:
         if (state == GLUT_DOWN)
            glutIdleFunc(spinDisplay);
         break;
      case GLUT_MIDDLE_BUTTON:
      case GLUT_RIGHT_BUTTON:
         if (state == GLUT_DOWN)
            glutIdleFunc(NULL);
         break;
      default:
         break;
   }
}
   
/* 
 *  Request double buffer display mode.
 *  Register mouse input callback functions
 */
int main(int argc, char** argv)
{
   int menu;

   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize (250, 250); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);

   menu=glutCreateMenu(menuResponse);
   glutAddMenuEntry("Spin Counterclockwise (+)", '+');
   glutAddMenuEntry("Spin Clockwise (-)", '-');
   glutAddMenuEntry("Stop Spinning (space)", ' ');
   glutAddMenuEntry("Quit (esc)", 27);
   glutAttachMenu(GLUT_RIGHT_BUTTON);

   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keypress);
   glutMouseFunc(mouse);
   glutMainLoop();
   return 0;   /* ANSI C requires main to return int. */
}
